# -*- encoding=utf8 -*-
__author__ = "chen"

import random

from common.common_util import *

def task_goin_zhuogui(idx):
    '''
    捉鬼任务，开始
    :param idx: 设备序列号
    :return: 无
    '''
    connect_device(f'Android:///{idx}')
    try:
        # 点击弹窗中的“捉鬼任务”
        if exists(Template(r"tpl1711162604668.png", record_pos=(0.335, -0.102), resolution=(635, 503))):
            pos = wait(Template(r"tpl1711162604668.png", record_pos=(0.33, -0.086), resolution=(635, 503)),20)
            if pos:
                app_touch(pos, [-20, 20], [-5, 5])


        if exists(Template(r"tpl1723946417330.png", record_pos=(0.332, -0.022), resolution=(494, 398))):
            pos = wait(Template(r"tpl1723946417330.png", record_pos=(0.332, -0.022), resolution=(494, 398)),20)
            if pos:
                pos[1] = pos[1] - 30
                app_touch(pos, [-20, 20], [-5, 5])


        # 点击任务列表中的 “捉鬼任务
        if exists(Template(r"tpl1711162801261.png", record_pos=(0.336, -0.204), resolution=(635, 503))):
            pos = wait(Template(r"tpl1711162801261.png", record_pos=(0.336, -0.204), resolution=(635, 503)),20)
            if pos:
                pos[1] = pos[1] + 20
                app_touch(pos, [-20, 20], [-5, 5])



    except Exception as e:
        G.LOGGING.info(e)
        G.LOGGING.info("未找到 捉鬼任务弹窗")
        filename = os.path.join(base_path, "zhuo'i", "sreenshot", f"{idx}-test_goin_zhuogui.png")
        snapshot(filename=filename, msg=f"{idx} 捉鬼任务，开始")
        return False

def task_second_zhuogui(idx):
    '''
    指定捉鬼设备，并开始捉鬼，并返回是否开始第2轮
    :param idx:
    :return:
    '''
    connect_device(f'Android:///{idx}')
    try:
        # 等待弹窗 做2次任务
        wait(Template(r"tpl1711162580677.png", record_pos=(0.102, 0.037), resolution=(635, 503)), 2000)
        pos = wait(Template(r"tpl1711162586493.png", record_pos=(0.098, 0.046), resolution=(635, 503)),10)
        app_touch(pos, [-20, 20], [-5, 5],times=1)
        task_goin_zhuogui(idx)
        return True
    except Exception as e:
        G.LOGGING.info(e)
        G.LOGGING.info("未找到 完成1轮捉鬼弹窗")
        filename = os.path.join(base_path, "zhuogui", "sreenshot", f"{idx}-test_second_zhuogui.png")
        snapshot(filename=filename, msg=f"{idx} 开始捉鬼，并返回是否开始第2轮")
        return False
def run_mask(idx,exp_counter):
    '''
    捉鬼任务 总体开始
    :param idx:
    :param exp_counter:
    :return:
    '''
    act_counter = 0
    while(exp_counter>act_counter):
        connect_device(f'Android:///{idx}')
        task_goin_zhuogui(idx)
        if task_second_zhuogui(idx):
            act_counter = act_counter + 1
        else:
            G.LOGGING.info(f"设备：{idx}，捉鬼结束，共捉鬼次数：{act_counter}")
            break


if __name__ == '__main__':
    # part1 获取有效设备
    get_json = get_config()
    device_list = ["bb6cc8c7","d90caf7b","127.0.0.1:16384","127.0.0.1:16416"] # 选择带队的设备序列号，只添加队长的设备
    exp_counter =get_json["zhuogui"] # 捉鬼次数

    # part2 执行捉鬼
    for i in range(len(device_list)):
        p = Process(target=run_mask,args=(device_list[i],exp_counter,))
        p.start()